package cn.spring.chapter04.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.stereotype.Component;

import java.lang.reflect.Modifier;

/**
 * GSON
 */
/*@Component
@Configuration*/
public class  GsonConfig{
    @Bean
    @ConditionalOnMissingBean
   public GsonHttpMessageConverter gsonHttpMessageConverter(){
        GsonHttpMessageConverter converter=new GsonHttpMessageConverter();
        GsonBuilder builder=new GsonBuilder();
        builder.setDateFormat("yyyy-MM-dd");
        //过滤protected字段
        builder.excludeFieldsWithModifiers(Modifier.PROTECTED);
        Gson gson=builder.create();
        converter.setGson(gson);
        return  converter;
    }
}
